//
// Created by yfux on 2022/4/18.
//

#include "BasePlayer.h"

BasePlayer::BasePlayer(int channelId, JavaCallHelper *helper, AVCodecContext *avCodecContext,
                       AVRational &time_base) :
        channelId(channelId),
        javaCallHelper(helper),
        avCodecContext(avCodecContext),
        time_base(time_base) {
    avPacket_queue.setReleaseHandle(releaseAvPacket);
    avFrame_queue.setReleaseHandle(releaseAvFrame);
}

void BasePlayer::setEnable(bool enable) {
    avPacket_queue.setEnable(enable);
    avFrame_queue.setEnable(enable);
}

void BasePlayer::releaseAvFrame(AVFrame *&avFrame) {
    if (avFrame) {
        av_frame_free(&avFrame);
        avFrame = 0;
    }
}

void BasePlayer::releaseAvPacket(AVPacket *&avPacket) {
    if (avPacket) {
        av_packet_free(&avPacket);
        avPacket = 0;
    }
}

BasePlayer::~BasePlayer() {
    if (avCodecContext) {
        avcodec_close(avCodecContext);
        avcodec_free_context(&avCodecContext);
        avCodecContext = NULL;
    }
    avPacket_queue.clear();
    avFrame_queue.clear();
}

BasePlayer::BasePlayer() {

}
